﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using ExaminationSys.DataAccess.Entity;
using DapperExtensions;
using SQLinq;
using SQLinq.Dapper;


namespace ExaminationSys.DataAccess.Repository
{
    public class SubSectionDetailInfoRepository : Repository<SubSectionDetailInfo>
    {
        public SubSectionDetailInfoRepository(SqlConnection context)
            : base(context)
        {
            db = context;
        }

        private SqlConnection db;

        public override SubSectionDetailInfo GetById(dynamic id)
        {
            return base.ObjectSet.Get<SubSectionDetailInfo>(db, id, null, null);
        }
    }

    public class SubSectionDetailInfoService
    {
        private SubSectionDetailInfoService()
        {
        }

        public List<SubSectionDetailInfo> GetAll()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SubSectionDetailInfo.GetList();
                return result.ToList();
            }
        }
        private  string SqlSafe( string source)
        {
            source = source.Replace("'", "''");
            source = source.Replace(",", "");
            source = source.Replace("\"", "");
            source = source.Replace("-", "");
            source = source.Replace("+", "");
            source = source.Replace("&", "");
            source = source.Replace("<", "");
            source = source.Replace(">", "");
            source = source.Replace("<>", "");
            return source;
        }
        public List<SubSectionDetailInfo> GetRelatedKnowledgeKeyWords(string keyWords)
        {
            if (string.IsNullOrEmpty(keyWords)) return new List<SubSectionDetailInfo>();
            keyWords = SqlSafe(keyWords);
            using (var db = DataContent.DBServer())
            {
                var sql =
                    string.Format(
                        "SELECT top 10 * FROM SubSectionDetailInfo t with(nolock) WHERE t.KnowledgeKeyWords LIKE '%{0}%' AND t.[Status]=1",
                        keyWords);
                var result =
                    db.Query<SubSectionDetailInfo>(sql).ToList();
                return result;
            }
        }

        public List<SubSectionDetailInfo> GetListBySubSectionId(Guid subSectionid)
        {
            using (var db = DataContent.DBServer())
            {
                var items = db.Query(from p in new SQLinq<SubSectionDetailInfo>()
                                     where p.SubSectionID == subSectionid && p.Status != 0
                                     select p).OrderBy(r => r.OrderIndex).ToList();
               
                return items;
            }
        }

        public List<SubSectionDetailInfo> GetPublishList()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var pg = new PredicateGroup {Operator = GroupOperator.And, Predicates = new List<IPredicate>()};
                pg.Predicates.Add(Predicates.Field<SubSectionDetailInfo>(f => f.Status, Operator.Eq, 1));
                var result = work.SubSectionDetailInfo.GetList(pg);
                return result.ToList();
            }
        }

        public bool CheckNameExists(string name, Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<SubSectionDetailInfo>(f => f.Name, Operator.Eq, name));
                pg.Predicates.Add(Predicates.Field<SubSectionDetailInfo>(f => f.ID, Operator.Eq, id, true));
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SubSectionDetailInfo.GetList(pg).FirstOrDefault();
                return result != null;
            }
        }
        public SubSectionDetailInfo Get(Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SubSectionDetailInfo.GetById(id);
                return result;
            }
        }
        public bool Update(SubSectionDetailInfo entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.SubSectionDetailInfo.Update(entity);
            }
        }
        public dynamic Insert(SubSectionDetailInfo entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.SubSectionDetailInfo.Insert(entity, null);
            }
        }
        public bool Delete(List<string> ids)
        {
            if (ids == null || ids.Count == 0) return false;
            bool flag = false;
            using (var db = DataContent.DBServer())
            {
                int retCode = 0;
                foreach (var id in ids)
                {
                    retCode = db.Execute(@"update SubSectionDetailInfo set status=0 where ID=@TmpId",
                                         new[] {new {TmpId = id}});
                    if (retCode <= 0)
                    {
                        break;

                    }
                }
                flag = true;
            }
            return flag;
        }

        public bool Lock(List<string> ids, bool _lock)
        {
            if (ids == null || ids.Count == 0) return false;
            bool flag = false;
            using (var db = DataContent.DBServer())
            {
                int retCode = 0;
                foreach (var id in ids)
                {
                    retCode = db.Execute(@"update SubSectionDetailInfo set Lock=@Lock where ID=@TmpId",
                                         new[] { new { Lock = _lock, TmpId = id } });
                    if (retCode <= 0)
                    {
                        break;

                    }
                }
                flag = true;
            }
            return flag;
        }
    }
}